package com.wicket.okrbff.web.contorller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.wicket.okrbff.biz.service.IntelligentCommunicationService;
import com.wicket.okrbff.biz.service.dto.req.*;
import com.wicket.okrbff.biz.service.dto.res.*;
import com.wicket.okrbff.common.ro.OkrResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.*;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wicket.okrcomponent.integration.FwCompIntelligentCommunicationClient;
import com.wicket.okrcomponent.integration.dto.ImplementIntelligentCommandCommunicationServiceComRespDto;
import com.wicket.okrcomponent.integration.dto.ImplementIntelligentCommandCommunicationServiceComReqDto;
import com.wicket.okrbff.common.exception.Assert;
import com.wicket.okrapp.integration.FwAppIntelligentCommunicationClient;
import com.wicket.okrapp.integration.dto.ImplementIntelligentCommandCommunicationServicesComRespDto;
import com.wicket.okrapp.integration.dto.ImplementIntelligentCommandCommunicationServicesComReqDto;

/**
 * @author ：
 * @date ：
 *
 * 
 * @version 1.0
 */
@Api(value = "IntelligentCommunication", tags = { "IntelligentCommunication" })
@RestController
// @RequestMapping("/oms/intelligentCommunication/")
public class IntelligentCommunicationController {

  @Autowired
  IntelligentCommunicationService intelligentCommunicationService;


    @Data
    static class ChatRequest {
        // 定义请求的属性，如app_id, api_key等
        private String app_id;
        private String api_key;
        private String api_secret;
        private String content;
        private String sn;

        // 省略getter和setter方法
    }

    @Data
    static class ChatResponse {
        private int code;
        private String msg;
        private ChatData data;

        // 构造方法和toJson方法
        public ChatResponse(int code, String msg, ChatData data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }

        public String toJson() {
            // 实现将对象转换为JSON字符串的逻辑

            JSONObject jo = new JSONObject();
            if(this.code==0){
                Map<String,Object> data = new HashMap<>();
                data.put("content",this.data.getContent());
                data.put("is_end",this.data.getIs_end());
                jo.put("code",this.code);
                jo.put("msg","success");
                jo.put("data",data);
                return jo.toJSONString();
            }else if(this.code==1){
                jo.put("code",this.code);
                jo.put("msg",this.msg);
                return jo.toJSONString();
            }
            return "";

        }


    }

    @Data
    static class ChatData {
        private String content;
        private Boolean is_end;

        public ChatData(String content) {
            this.content = content;
            this.is_end = true;
        }

        // 省略getter和setter方法
    }

    // TODO: 2023/12/10 对接乐天派，强制改下代码 
///**
//   * B2执行智能指令沟通服务
//   * gen by moon at 12/10/2023, 7:45:57 PM
//   */
//    @PostMapping("/oms/intelligentCommunication/implementIntelligentCommandCommunicationService")
//    @ApiOperation(value = "B2执行智能指令沟通服务", response = OkrResult.class)
//    public OkrResult<BffImplementIntelligentCommandCommunicationServiceRespDto> implementIntelligentCommandCommunicationService(@RequestBody(required=false) @Validated BffImplementIntelligentCommandCommunicationServiceReqDto reqDTO){
//        return OkrResult.success(intelligentCommunicationService.implementIntelligentCommandCommunicationService(reqDTO));
//    }

    /**
     * B2执行智能指令沟通服务
     * gen by moon at 12/10/2023, 7:45:57 PM
     */
    @PostMapping("/oms/intelligentCommunication/implementSmartSession")
    @ApiOperation(value = "B2执行智能指令沟通服务", response = OkrResult.class)
    public String chat(@RequestBody ChatRequest request) {
        // 打印接收到的参数，用于调试
        try{
            BffImplementIntelligentCommandCommunicationServiceReqDto reqDto = new BffImplementIntelligentCommandCommunicationServiceReqDto();
            reqDto.setApiKey(request.getApi_key());
            reqDto.setAppId(request.getApp_id());
            reqDto.setApiSecret(request.getApi_secret());
            reqDto.setSn(request.getSn());
            reqDto.setContent(request.getContent());
            BffImplementIntelligentCommandCommunicationServiceRespDto retData = intelligentCommunicationService.implementIntelligentCommandCommunicationService(reqDto);
            // 构建响应数据
            ChatResponse response = new ChatResponse(0, "Request processed successfully",
                    new ChatData(retData.getContent()));

            // 将响应数据转换为JSON字符串
            String jsonResponse = response.toJson();

            // 返回响应
            return "event: message\ndata: " + jsonResponse + "\n\n";
        }catch (Exception e){
            // 构建响应数据
            ChatResponse response = new ChatResponse(1, e.getMessage(), null);

            // 将响应数据转换为JSON字符串
            String jsonResponse = response.toJson();

            // 返回响应
            return "event: message\ndata: " + jsonResponse + "\n\n";
        }

    }
/**
   * B3执行记录想法
   * gen by moon at 1/21/2024, 8:20:02 PM
   */
    @PostMapping("/oms/intelligentCommunication/implementRecordIdeas")
    @ApiOperation(value = "B3执行记录想法", response = OkrResult.class)
    public OkrResult<BffImplementRecordIdeasRespDto> implementRecordIdeas(@RequestBody(required=false) @Validated BffImplementRecordIdeasReqDto reqDTO){
        return OkrResult.success(intelligentCommunicationService.implementRecordIdeas(reqDTO));
    }
}
